package com.yuandian.business.monitor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.business_monitor.po.LongProcessParams;
import com.yuandian.api.business_monitor.vo.LongProcessDetailVo;
import com.yuandian.api.business_monitor.vo.LongProcessExcelVo;
import com.yuandian.api.business_monitor.vo.LongProcessOverviewVo;
import com.yuandian.business.monitor.mapper.BusinessWorkFlowRecordMapper;
import com.yuandian.business.monitor.service.ILongProcessService;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 说明：长流程业务service实现
 *
 * @Author yanjun
 * @Date 2020/9/29 13:29
 */
@Slf4j
@Service
public class LongProcessServiceImpl implements ILongProcessService {

    @Autowired
	BusinessWorkFlowRecordMapper businessWorkFlowRecordMapper;

    /**
     * 分页查询长流程概况列表
     *
     * @param params 长流程业务查询对象
     * @return 长流程概况列表
     */
    @Override
    public PageDataInfo pageLongProcessOverview(LongProcessParams params) {
        Page page = new Page<>(params.getPageNum(), params.getPageSize());
        List<LongProcessOverviewVo> list = businessWorkFlowRecordMapper.queryWorkFlowRecord(page, params);
        return new PageDataInfo(page.getTotal(), list);
    }

    /**
     * 查询长流程详情列表
     *
     * @param params 长流程业务查询对象
     * @return 长流程详情列表
     */
    @Override
    public List<LongProcessDetailVo> queryLongProcessDetail(LongProcessParams params) {
        List<LongProcessDetailVo> lpdVoList = new ArrayList<>();


        Page page = new Page(1,100000L);
        List<LongProcessDetailVo> list = businessWorkFlowRecordMapper.queryLongProcessDetail(page, params);

        return list;
    }

    /**
     * 长流程业务导出Excel
     *
     * @param params   长流程业务查询对象
     * @param response 响应上下文
     */
    @Override
    public void exportExcel(LongProcessParams params, HttpServletResponse response) throws IOException {
        List<LongProcessExcelVo> excelVoList = new ArrayList<>();
        // 查询所有数据
        params.setPageNum(1L);
        params.setPageSize(100000L);
        List<LongProcessOverviewVo> lpoVoList = (List<LongProcessOverviewVo>) pageLongProcessOverview(params).getData();
        lpoVoList.forEach(lpoVo -> {
            LongProcessExcelVo excelVo = new LongProcessExcelVo();
            excelVo.setSysProcessId(lpoVo.getSysProcessId()); // 业务流转号
            excelVo.setStartTime(lpoVo.getStartTime()); // 业务开始时间
            excelVoList.add(excelVo);

            LongProcessParams condition = new LongProcessParams();
            condition.setSysProcessId(lpoVo.getSysProcessId());
            List<LongProcessDetailVo> lpdVoList = queryLongProcessDetail(condition);
            lpdVoList.forEach(lpdVo -> {
                LongProcessExcelVo subExcelVo = new LongProcessExcelVo();
                subExcelVo.setSysProcessId(lpoVo.getSysProcessId()); // 业务流转号
                subExcelVo.setStartTime(lpoVo.getStartTime()); // 业务开始时间
                subExcelVo.setBusinessName(lpdVo.getBusinessName()); // 业务名称
                subExcelVo.setNodeStartTime(lpdVo.getStartTime()); // 节点操作时间
                subExcelVo.setNodeEndTime(lpdVo.getEndTime()); // 节点结束时间
                subExcelVo.setDealState(lpdVo.getDealState() == 1 ? "成功" : "失败"); // 业务状态
                subExcelVo.setExclusiveIndicator(lpdVo.getExclusiveIndicator()); // 业务详情
                excelVoList.add(subExcelVo);
            });
        });
        ExcelUtil.writeExcel(LongProcessExcelVo.class, "长流程业务回溯列表", "长流程业务回溯列表", excelVoList, response);
    }
}
